package com.example.jnet.pojo;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;

import java.util.Arrays;

@Data
public class MessageProtocol {
    public final static int DEFAULT_HEADER_LENGTH = 16;

    public final static int PROTOCOL_REGISTER = 1;
    public final static int PROTOCOL_ACK = 2;
    public final static int PROTOCOL_PRACK = 3;
    public final static int PROTOCOL_CANCEL = 4;
    public final static int PROTOCOL_INVITE = 5;
    public final static int PROTOCOL_BYE = 6;
    public final static int PROTOCOL_OPTIONS = 7;


    private byte[] type = new byte[4];

    private byte[] id = new byte[4];

    private byte[] cmd = new byte[4];

    private byte[] length = new byte[4];

    private byte[] body;

    public ByteBuf createByteBuf() {
        ByteBuf buf = Unpooled.buffer(0);

        if (this.getType() != null) {
            buf.writeBytes(this.getType());
        }

        if (this.getId() != null) {
            buf.writeBytes(this.getId());
        }

        if (this.getCmd() != null) {
            buf.writeBytes(this.getCmd());
        }

        if (this.getLength() != null) {
            buf.writeBytes(this.getLength());
        }

        if (this.getBody() != null) {
            buf.writeBytes(this.getBody());
        }

        return buf;
    }

}
